package au.com.bus4u.service.impl;

import au.com.bus4u.dao.DaoSupport;
import au.com.bus4u.exception.ServiceException;
import au.com.bus4u.service.MenuService;
import au.com.bus4u.utils.PageData;
import lombok.extern.java.Log;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Log
@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Autowired
    private DaoSupport daoSupport;
    private String mapper = "MenuMapper.";


    @Override
    public int save(PageData pd) throws Exception {

        checkNoNullData(pd);

        pd.remove("menu_id");
        pd.put("menu_type",1);

        return (int) daoSupport.save(mapper+"save", pd);
    }
    private void checkNoNullData(PageData pd) throws Exception  {
        if( pd.get("menu_pid") ==null ||"".equals(pd.get("menu_pid").toString())){
            throw new ServiceException("添加菜单失败:父级菜单ID不能空");
        }
        if( pd.get("menu_name") ==null ||"".equals(pd.get("menu_name").toString())){
            throw new ServiceException("添加菜单失败:菜单名称不能空");
        }
        if( pd.get("menu_status") ==null ||"".equals(pd.get("menu_status").toString())){
            throw new ServiceException("添加菜单失败:menu_status不能空");
        }
        if( pd.get("menu_sort") ==null ||"".equals(pd.get("menu_sort").toString())){
            throw new ServiceException("添加菜单失败:menu_sort不能空");
        }
        if( pd.get("spread") ==null ||"".equals(pd.get("spread").toString())){
            throw new ServiceException("添加菜单失败:spread不能空");
        }

    }

    @Override
    public int delete(Integer menu_id) throws Exception {
        PageData pd = new PageData();

        //删除菜单之前需要判断是否有角色使用了此菜单
        int count = (int) daoSupport.findForObject(mapper+"findRoleCountByMenuID", menu_id);
        log.info("通过菜单ID查询到使用了此菜单的角色数量为:" + count);
        if(count != 0){
            throw new ServiceException("删除菜单失败:有角色使用了此菜单,如需删除请先取消掉相应角色的权限分配");
        }

        pd.put("menu_id",menu_id);
        return (int) daoSupport.delete(mapper+"deleteById", pd);
    }

    @Override
    public int edit(PageData pd) throws Exception {
        return (int) daoSupport.update(mapper+"edit", pd);
    }

    @Override
    public List<PageData> list(PageData pd) throws Exception {
        pd.put("menu_type",1);
        if(pd.get("menu_name") ==null|| "".equals(pd.get("menu_name").toString()) ){
            pd.remove("menu_name");
        }
        return (List<PageData>) daoSupport.findForList(mapper+"list",pd);
    }
    @Override
    public List<PageData> loadAllMenu(PageData pd) throws Exception {
        pd.put("menu_type",1);
        if(pd.get("menu_name") ==null|| "".equals(pd.get("menu_name").toString()) ){
            pd.remove("menu_name");
        }
        return (List<PageData>) daoSupport.findForList(mapper+"loadAllMenu",pd);
    }

    @Override
    public Integer queryMaxSortNum() throws Exception {
        return (Integer)daoSupport.findForObject(mapper+"queryMenuMaxSortNum");
    }

    @Override
    public Integer checkMenuHasChildrenNode(Integer menu_id) throws Exception {
        if(menu_id == null){
            throw new ServiceException("menu_id不能空");
        }
        return (Integer)daoSupport.findForObject(mapper+"checkMenuHasChildrenNode",menu_id);
    }

    @Override
    public PageData findById(Integer menu_id) throws Exception {
        PageData pd = new PageData();
        pd.put("menu_id",menu_id);

        daoSupport.findForObject(mapper+"deleteRoleMenuByMenuId",pd);//删除关联表role_menu中的数据

        return (PageData) daoSupport.findForObject(mapper+"list",pd);
    }



    @Override
    public int deleteAll(String[] DATA_IDS) throws Exception {

        return (int)daoSupport.batchDelete(mapper+"deleteAll", DATA_IDS);
    }

    @Override
    public List<PageData> findByPId(Integer menu_pid) throws Exception {
        PageData pd = new PageData();
        pd.put("menu_pid",menu_pid);
        return (List<PageData>) daoSupport.findForList(mapper+"list",pd);
    }

    @Override
    public List<PageData> findByRoleIdAndMenuPId(Integer role_id, Integer menu_pid) throws Exception {
        List<PageData> list= new ArrayList<>();
        //通过父级菜单id查询子菜单list
        List<PageData> roleMenu = findByPId(menu_pid);
//        System.err.println("roleMenu = " + roleMenu);
        //遍历子菜单list获取子菜单id
        PageData pd = new PageData();
        pd.put("role_id",role_id);
        for (PageData menu:roleMenu){
            pd.put("menu_id",menu.getInt("menu_id"));
            PageData forObject = (PageData) daoSupport.findForObject(mapper + "findByRoleIdAndMenuId", pd);
            if(forObject != null){
                pd.remove("menu_id");
                List<PageData> children = new ArrayList<>();
                if( forObject != null){
                    children = findByRoleIdAndMenuPId(role_id, forObject.getInt("menu_id"));
                }
                forObject.put("children",children);
                list.add(forObject);
            }
        }
        return list;
    }

    /**
     * 获取菜单tree数据
     * @return
     * @throws Exception
     */
    @Override
    public List<PageData> loadMenuTree(Integer menu_pid) throws Exception {
        PageData pd = new PageData();
        pd.put("menu_pid",menu_pid);
//        pd.put("menu_type",1);
        //通过父级菜单id查询子菜单list
        List<PageData> roleMenu = (List<PageData>) daoSupport.findForList(mapper + "findMenuTreeListByMenuPId", pd);
        for (PageData menu:roleMenu){
            List<PageData> children = new ArrayList<>();
            children = loadMenuTree(menu.getInt("id"));
            menu.put("children",children);
            menu.put("spread",menu.getInt("spread") == 0 ? false : true);
        }
        return roleMenu;
    }




    private PageData getCurrentUser(){
        return (PageData) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
    }
    private String getCurrentUserID(){
        PageData pd = getCurrentUser();
        String user_id = pd.get("user_id").toString();
        return user_id;

    }
    private Integer getCurrentUserRoleID(){
        PageData pd = getCurrentUser();
        Integer role_id = pd.getInt("role_id");
        return role_id;

    }




}
